package com.zw.re.confi;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisSentinelConfiguration;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericToStringSerializer;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import redis.clients.jedis.JedisPoolConfig;

import java.io.Serializable;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;

@Configuration
@ConfigurationProperties(prefix="myRedis.sentinel")
public class RedisConnectionConfig {

    public static final Logger log = LoggerFactory.getLogger(RedisConnectionConfig.class);

    private String urls;

    private String masterName;

    @Bean
    public RedisSentinelConfiguration redisSentinelConfiguration() throws Exception {
        if (StringUtils.isEmpty(this.urls)) {
            throw new Exception("RedisConnectionConfig 初始化 Sentinel url 错误.");
        } else {
            List urlList = Arrays.asList(this.urls.split(","));
            if (!CollectionUtils.isEmpty(urlList) && !StringUtils.isEmpty(this.masterName)) {
                if (StringUtils.isEmpty(this.masterName)) {
                    throw new Exception("RedisConnectionConfig 初始化 masterName 错误.");
                } else {
                    RedisSentinelConfiguration redisSentinelConfiguration = null;

                    try {
                        HashSet e = new HashSet();
                        e.addAll(urlList);
                        redisSentinelConfiguration = new RedisSentinelConfiguration(this.masterName, e);
                        return redisSentinelConfiguration;
                    } catch (Exception var4) {
                        log.error(var4.getMessage());
                        throw var4;
                    }
                }
            } else {
                throw new Exception("RedisConnectionConfig 初始化 Sentinel url 错误.");
            }
        }
    }

    @Bean
    @ConfigurationProperties(prefix = "myRedis.JedisPoolConfig")
    public JedisPoolConfig jedisPoolConfig() {
        JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
        log.info("init jedisPoolConfig success....");
        return jedisPoolConfig;
    }

    @Bean
    @ConfigurationProperties(prefix = "myRedis.jedisConnectionFactoryConf")
    public JedisConnectionFactory jedisConnectionFactory(RedisSentinelConfiguration redisSentinelConfiguration, JedisPoolConfig jedisPoolConfig) {
        JedisConnectionFactory jedisConnectionFactory = new JedisConnectionFactory(redisSentinelConfiguration, jedisPoolConfig);
        log.info("init jedisConnectionFactory success....");
        return jedisConnectionFactory;
    }

    @Bean
    public RedisSerializer jackson2JsonRedisSerializer() {
        return new Jackson2JsonRedisSerializer(Serializable.class);
    }

    @Bean
    public RedisSerializer genericToStringSerializer() {
        return new GenericToStringSerializer(Object.class);
    }

    @Bean
    public RedisSerializer stringRedisSerializer() {
        return new StringRedisSerializer();
    }


    @Bean
    public RedisTemplate redisTemplate(JedisConnectionFactory jedisConnectionFactory,
                                       RedisSerializer genericToStringSerializer) {
        RedisTemplate redisTemplate = new RedisTemplate();
        /*
            默认序列化 仅仅针对要保存的是String类型的数据
            如果是其他的数据类型保存需要重新设置序列化对象，
            否则抛出SerializationException
         */
        redisTemplate.setDefaultSerializer(genericToStringSerializer);
        redisTemplate.setHashKeySerializer(genericToStringSerializer);
        redisTemplate.setConnectionFactory(jedisConnectionFactory);
        log.info("init RedisTemplate success....");
        return redisTemplate;
    }

    public String getUrls() {
        return urls;
    }

    public void setUrls(String urls) {
        this.urls = urls;
    }

    public String getMasterName() {
        return masterName;
    }

    public void setMasterName(String masterName) {
        this.masterName = masterName;
    }
}
